package com.bodu.depress.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.core.common.AbstractCommonController;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.result.PageResult;
import com.bodu.core.entity.result.Result;
import com.bodu.depress.entity.request.DepressQuestionOptionsSolutionRequest;
import com.bodu.global.security.LoginUserUtil;
import com.bodu.global.annon.TraceLog;
import com.bodu.depress.entity.vo.DepressQuestionVo;
import com.bodu.depress.entity.request.DepressQuestionRequest;
import com.bodu.depress.service.DepressQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 题目 API
* @author Administrator
*/
@Api(tags = "题目 API")
@RestController
@RequestMapping("/depressQuestion")
public class DepressQuestionController extends AbstractCommonController<DepressQuestionService, DepressQuestionRequest, DepressQuestionVo>{

    private final DepressQuestionService depressQuestionService;

    public DepressQuestionController(DepressQuestionService depressQuestionService) {
        this.depressQuestionService = depressQuestionService;
    }

    @TraceLog()
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(depressQuestionService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存题目")
    public Result<Integer> save(@RequestBody DepressQuestionRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.save(depressQuestionService, request);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新题目")
    public Result<Integer> update(@RequestBody DepressQuestionRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.update(depressQuestionService, request);
    }


    @TraceLog()
    @PostMapping("saveOrUpdateAll")
    @ApiOperation("更新题目")
    public Result<Integer> saveOrUpdateAll(@RequestBody DepressQuestionOptionsSolutionRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        // 新增
        if (ObjectUtil.equal(request.getAddDataType(), "add")) {
            return depressQuestionService.saveAll(request);
        } else if (ObjectUtil.equal(request.getAddDataType(), "update")) {
            // xiug
            return depressQuestionService.updateAll(request);
        }
        return Result.fail("未发现该类型");
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("题目-查询")
    public Result<List<DepressQuestionVo>>listPage(@RequestBody ParamReq<DepressQuestionRequest> req) {
        return super.listPage(depressQuestionService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("题目-统计")
    public Result<Integer> countPage(@RequestBody ParamReq<DepressQuestionRequest> req) {
        return super.countPage(depressQuestionService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("题目-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<DepressQuestionRequest> req) {
        return super.page(depressQuestionService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除题目")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(depressQuestionService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除题目")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(List<Long> ids) {
        return super.batchDelete(depressQuestionService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }

}
